package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.company.dao.DepartmentDao;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.fz.us.admin.company.entity.Department;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Dao 实现类 - 部门
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-27 。
 * ============================================================================
 */
@Repository
public class DepartmentDaoImpl extends BaseDaoImpl<Department, String> implements DepartmentDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getAllDepartments(Company company) {
		Assert.notNull(company, "company is required");
		String hql = "from " + Department.class.getName() + " model where model.company = :company and model.state = :state order by model.sortNo asc ";
		return getSession().createQuery(hql).setParameter("company", company).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getRootDepartments(Company company){
		Assert.notNull(company, "company is required");
		String hql = "from " + Department.class.getName() + " model where model.company = :company and model.parent is null and model.state = :state order by model.sortNo asc ";
		return getSession().createQuery(hql).setParameter("company", company).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments(Company company, Department department) {
		Assert.notNull(company, "company is required");
		Assert.notNull(department, "department is required");
		String hql = "from " + Department.class.getName() + " model where model.company = :company and model.parent = :parent and model.state = :state order by model.sortNo asc ";
		return getSession().createQuery(hql).setParameter("company", company).setParameter("parent", department).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
	}

	@Override
	public boolean isParentDepartment(Department department) {
		String hql;
		if (department == null) {
			hql = "select count(*) from " + Department.class.getName() + " model where model.parent is null and model.state = :state";
			return ((Long) getSession().createQuery(hql).setParameter("state", BaseEnumManager.StateEnum.Enable).uniqueResult()) > 0;
		} else {
			hql = "select count(*) from " + Department.class.getName() + " model where model.parent = :parent and model.state = :state";
			return ((Long) getSession().createQuery(hql).setParameter("parent", department).setParameter("state", BaseEnumManager.StateEnum.Enable).uniqueResult()) > 0;
		}
	}

    @Override
    public List<Department> getDepartments(String name) {
		Assert.notNull(name,"name is required");
		String hql="from " + Department.class.getName() + " as d where d.name=:n and d.state=:s";
		return getSession().createQuery(hql).setParameter("n",name).setParameter("s",  BaseEnumManager.StateEnum.Enable).list();
    }

}
